/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject.selectQuestion;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;

import ru.irenproject.AbstractQuestionEditor;
import ru.irenproject.Action;
import ru.irenproject.Check;
import ru.irenproject.Question;
import ru.irenproject.TestInputException;
import ru.irenproject.pad.PadEditor;
import ru.irenproject.store.Event;
import ru.irenproject.store.Stash;
import ru.irenproject.store.Thing;

import com.google.protobuf.Message;

public final class SelectQuestionEditor extends AbstractQuestionEditor {
  public static final class AddNegativeChoiceInput {
    public String text;
  }

  public static final class DeleteChoiceInput {
    public int choiceIndex;
  }

  public static final class MoveChoiceInput {
    public int choiceIndex;
    public boolean forward;
  }

  @Stash private SelectQuestionProtos.SelectQuestionEditor.Builder $;

  @Inject SelectQuestionEditor() {}

  @Override protected void setQuestion(Question question) {
    Validate.isInstanceOf(SelectQuestion.class, question);
    $.setQuestion(question.id());
    $.setFormulationEditor(PadEditor.create(question().formulation()).id());
    question().addListener(this);
    syncChoiceEditors();
  }

  @Override public SelectQuestion question() {
    return get($.getQuestion(), SelectQuestion.class);
  }

  @Override public Message render() {
    return SelectQuestionProtos.SelectQuestionEditorShape.newBuilder()
        .setFormulationEditor($.getFormulationEditor())
        .addAllChoiceEditor($.getChoiceEditorList())
        .setCanAddChoice(question().canAddChoice())
        .setCanAddNegativeChoice(canAddNegativeChoice())
        .build();
  }

  @Override protected void handleEvent(Event e, Thing source) {
    if (e instanceof SelectQuestion.ChangeEvent) {
      syncChoiceEditors();
    }
  }

  private void syncChoiceEditors() {
    $.clearChoiceEditor();
    for (SelectQuestionChoice choice : question().choiceList()) {
      $.addChoiceEditor(SelectQuestionChoiceEditor.open(choice).id());
    }
  }

  @Action(badInput = TestInputException.class)
  public void doAddChoice() {
    question().addChoice(false);
  }

  @Action(badInput = TestInputException.class)
  public void doDeleteChoice(DeleteChoiceInput in) {
    question().deleteChoice(in.choiceIndex);
  }

  @Action(badInput = TestInputException.class)
  public void doMoveChoice(MoveChoiceInput in) {
    question().moveChoice(in.choiceIndex, in.forward);
  }

  private boolean canAddNegativeChoice() {
    return question().canAddChoice() && !question().hasNegativeChoice();
  }

  @Action(badInput = TestInputException.class)
  public void doAddNegativeChoice(AddNegativeChoiceInput in) {
    Check.inputNotNull(in.text);

    SelectQuestionChoice c = question().addChoice(true);
    c.setFixed(true);
    c.pad().appendText(in.text);
  }
}
